clear

* set the directory
cd ".../SocialPrefs_KantianMorality_ReplicationPackage"


clear 
use "allresults.dta"
gen type=0
replace type=1 if game_type=="SPD"
replace type=2 if game_type=="UG"
replace type=3 if game_type=="TG"

preserve 
keep if decision=="actions"

gen x1=0
gen x2=0
gen x3=0

replace x1=1 if game_type=="SPD" & keuzea=="RIGHT"
replace x2=1 if game_type=="SPD" & keuzebr=="NORTH"
replace x3=1 if game_type=="SPD" & keuzebl=="WEST"

replace x1=1 if game_type=="TG" & keuzea=="RIGHT"
replace x2=1 if game_type=="TG" & keuzebr=="NORTH"

replace x1=1 if game_type=="UG" & keuzea=="LEFT"
replace x2=1 if game_type=="UG" & keuzebr=="EAST"

gen choice=0
replace choice=1 if game_type=="SPD" & x1==1 & x2==1 & x3==1
replace choice=2 if game_type=="SPD" & x1==1 & x2==1 & x3==0
replace choice=3 if game_type=="SPD" & x1==1 & x2==0 & x3==1
replace choice=4 if game_type=="SPD" & x1==1 & x2==0 & x3==0
replace choice=5 if game_type=="SPD" & x1==0 & x2==1 & x3==1
replace choice=6 if game_type=="SPD" & x1==0 & x2==1 & x3==0
replace choice=7 if game_type=="SPD" & x1==0 & x2==0 & x3==1
replace choice=8 if game_type=="SPD" & x1==0 & x2==0 & x3==0

replace choice=1 if game_type=="TG" & x1==1 & x2==1 
replace choice=2 if game_type=="TG" & x1==1 & x2==0 
replace choice=3 if game_type=="TG" & x1==0 & x2==1 
replace choice=4 if game_type=="TG" & x1==0 & x2==0 

replace choice=1 if game_type=="UG" & x1==1 & x2==1 
replace choice=2 if game_type=="UG" & x1==1 & x2==0 
replace choice=3 if game_type=="UG" & x1==0 & x2==1 
replace choice=4 if game_type=="UG" & x1==0 & x2==0 

keep id round x1 x2 x3 choice

save "actions.dta", replace

restore

keep if decision=="beliefs"

gen y1=0 
gen y2=0
gen y3=0

destring keuzea keuzebl keuzebr, replace

replace y1=keuzea/100 if game_type=="SPD"
replace y2=1-keuzebr/100 if game_type=="SPD"
replace y3=1-keuzebl/100 if game_type=="SPD"

replace y1=keuzea/100 if game_type=="TG"
replace y2=1-keuzebr/100 if game_type=="TG"

replace y1=1-keuzea/100 if game_type=="UG"
replace y2=keuzebr/100 if game_type=="UG"

merge 1:1 id round using "actions.dta"
drop _merge

sort id round

save "hm_all.dta", replace

erase "actions.dta"

* Export the data for the finite mixture estimations
use "hm_all.dta", clear
preserve
keep id game_no t r p s y1 y2 y3 choice type
order id game_no t r p s y1 y2 y3 choice
save "mnl_data.dta", replace
export delimited using "mnl_data.csv", novarnames replace
restore

preserve
egen y1_re = mean(x1), by(game_no)
egen y2_re = mean(x2), by(game_no)
egen y3_re = mean(x3), by(game_no)
keep id game_no t r p s y1_re y2_re y3_re choice type
rename y1_re y1
rename y2_re y2
rename y3_re y3
order id game_no t r p s y1 y2 y3 choice
save "mnl_data_re.dta", replace
export delimited using "mnl_data_re.csv", novarnames replace
restore

use "allsubjects.dta", clear
keep risk
export delimited using "risk.csv", novarnames replace
clear

* Export the data of the 'core sample' for the finite mixture estimations
use "hm_estimates.dta", clear
preserve
keep id core
save "core.dta", replace
restore

use "mnl_data.dta", clear
merge m:1 id using "core.dta"
keep if core==1
drop  _merge core
export delimited using "mnl_data_core.csv", novarnames replace
clear

erase "mnl_data.dta"

* Also for 'rational expectations'
use "mnl_data_re.dta", clear
merge m:1 id using "core.dta"
keep if core==1
drop  _merge core
export delimited using "mnl_data_re_core.csv", novarnames replace
clear

erase "mnl_data_re.dta"
